package com.wcl.demo.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wcl.demo.R;
import com.wcl.demo.db.CanSendHelper;
import com.wcl.demo.db.UserInforHelper;
import com.wcl.demo.model.entity.CanSendData;
import com.wcl.demo.model.entity.UserInfor;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/12/3.
 */
public class MinePrivateActivity extends Activity implements View.OnClickListener {
    private ImageView activity_mine_private_bacIv;
    private TextView mine_private_name, mine_private_work, mine_private_school,
            mine_private_birthday, mine_private_phoneNumber, mine_private_city;
    private UserInforHelper userInforHelper;
    private CanSendHelper canSendHelper;
    private Button mine_private_saveBtn;
    private CheckBox mine_private_name_chex, mine_private_workChex, mine_private_schoolChex,
            mine_private_birthdayChex, mine_private_phoneNumberChex, mine_private_cityChex;
    private String canSend = "";
    private List<UserInfor> list;
    private List<CanSendData> canSendDatas;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_private);

        initView();
    }

    private void initView() {
        userInforHelper = new UserInforHelper();
        list = userInforHelper.queryAll();
        canSendHelper = new CanSendHelper();
        canSendDatas = canSendHelper.queryAll();

        activity_mine_private_bacIv = (ImageView) findViewById(R.id.activity_mine_private_bacIv);
        activity_mine_private_bacIv.setOnClickListener(this);

        mine_private_name = (TextView) findViewById(R.id.mine_private_name);
        mine_private_work = (TextView) findViewById(R.id.mine_private_work);
        mine_private_school = (TextView) findViewById(R.id.mine_private_school);
        mine_private_birthday = (TextView) findViewById(R.id.mine_private_birthday);
        mine_private_phoneNumber = (TextView) findViewById(R.id.mine_private_phoneNumber);
        mine_private_city = (TextView) findViewById(R.id.mine_private_city);

        mine_private_saveBtn = (Button) findViewById(R.id.mine_private_saveBtn);
        mine_private_saveBtn.setOnClickListener(this);


        mine_private_name_chex = (CheckBox) findViewById(R.id.mine_private_name_chex);
        mine_private_workChex = (CheckBox) findViewById(R.id.mine_private_workChex);
        mine_private_schoolChex = (CheckBox) findViewById(R.id.mine_private_schoolChex);
        mine_private_birthdayChex = (CheckBox) findViewById(R.id.mine_private_birthdayChex);
        mine_private_phoneNumberChex = (CheckBox) findViewById(R.id.mine_private_phoneNumberChex);
        mine_private_cityChex = (CheckBox) findViewById(R.id.mine_private_cityChex);


        if (userInforHelper != null&&list!=null&&list.size()>0) {
            //个人Chex
            mine_private_name_chex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        canSend = canSend + ("个人:" + list.get(0).getUser_person() + " ");
                    } else {
                        canSend = canSend.replace("个人:" + list.get(0).getUser_person() + " ", "");
                    }
                }
            });
            //职业
            mine_private_workChex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        canSend = canSend + ("职业:" + list.get(0).getUser_work() + " ");
                    } else {
                        canSend = canSend.replace("职业:" + list.get(0).getUser_work() + " ", "");
                    }
                }
            });
            //学校
            mine_private_schoolChex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        canSend = canSend + ("学校:" + list.get(0).getUser_school() + " ");
                    } else {
                        canSend = canSend.replace("学校:" + list.get(0).getUser_school() + " ", "");
                    }
                }
            });
            //生日
            mine_private_birthdayChex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        canSend = canSend + ("生日:" + list.get(0).getUser_birthday() + " ");
                    } else {
                        canSend = canSend.replace("生日:" + list.get(0).getUser_birthday() + " ", "");
                    }
                }
            });
            //电话
            mine_private_phoneNumberChex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        canSend = canSend + ("电话:" + list.get(0).getUser_phone() + " ");
                    } else {
                        canSend = canSend.replace("电话:" + list.get(0).getUser_phone() + " ", "");
                    }
                }
            });
            //地区
            mine_private_cityChex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        canSend = canSend + ("地区:" + list.get(0).getUser_parent_area() + " ");
                    } else {
                        canSend = canSend.replace("地区:" + list.get(0).getUser_parent_area() + " ", "");
                    }
                }
            });
            mine_private_name.setText(list.get(0).getUser_person());
            mine_private_work.setText(list.get(0).getUser_work());
            mine_private_school.setText(list.get(0).getUser_school());
            mine_private_birthday.setText(list.get(0).getUser_birthday());
            mine_private_phoneNumber.setText(list.get(0).getUser_phone());
            mine_private_city.setText(list.get(0).getUser_parent_area());
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_mine_private_bacIv:
                finish();
                break;
            case R.id.mine_private_saveBtn:
                if (userInforHelper != null&&list!=null&&list.size()>0) {
                    if (canSendDatas != null && canSendDatas.size() > 0) {
                        canSendHelper.deleteAll();
                    }
                    List<CanSendData> canSendDatas = new ArrayList<>();
                    CanSendData canSendData = new CanSendData();
                    canSendData.setCanSend(canSend);
                    canSendDatas.add(canSendData);
                    canSendHelper.saveCanSend(canSendDatas);
                    finish();


                    Log.i("shaco", canSendHelper.toString() + "");
                }else {
                    Toast.makeText(this,"请先去完善个人资料",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
